package beyjava.util;

import java.text.DateFormat;

import java.text.ParsePosition;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * @author BEYJAVA
 * some controls for inputs from users
 */
public class Common {	
	 /**
	 * @param low
	 * @param high
	 * @return choose choose of user
	 * @throws IllegalArgumentException, InputMismatchException
	 * method to get choose of user from console
	 */
	public static int  getChoose(int low ,int high){
		   int choose=-1 ;
		   Scanner sc = new Scanner(System.in);
		   try{
			choose = sc.nextInt();
			Validator.checkForRange(choose, low,high);
			} catch (IllegalArgumentException e) {
				System.out.println(e.getMessage());
				return getChoose(low,high);
			} catch (InputMismatchException e){
				System.out.println("Input must be integer number.Please enter again.");
				return getChoose(low, high);
			}
			return choose;
    }
	/**
	 * @param format
	 * @return date console scanner
	 * @throws Exception
	 * method to get date entrance from user
	 */
	public static Date getDate(String format){ 
         Date date = null;    
 	   try{
 		   	Scanner sc = new Scanner(System.in);
            String projectStartDate = sc.next();
            Validator.isValidDate(projectStartDate);
            DateFormat formatter ; 
            formatter = new SimpleDateFormat(format);
            date = (Date)formatter.parse(projectStartDate);
       }catch(Exception e){
 				 System.out.println("Exception : "+e.getMessage());
 				 return getDate(format);
 			 }
	 return date;
     }
	 /**
	 * @param tarih_zaman
	 * @param format
	 * @return d date 
	 * method to convert string date to format date
	 */
	public static Date convertToDate(String tarih_zaman, String format) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
	    Date d = null;
		ParsePosition pos = new ParsePosition(0);
		d = dateFormat.parse(tarih_zaman,pos);
		return d;
	}
	/**
	 * @param date the date object to be converted 
	 * @param format the format of date to be converted
	 * @return converted date in given format 
	 */
	public static String convertDateToStringFormat(Date date , String format){
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		String dateString = dateFormat.format(date);
		return dateString;
	}
	/**
	 * @return value input number from user
	 * @throws InputMismatchException
	 * @throws IllegalArgumentException
	 * method to force user to enter new number
	 */
	public static int getInteger(){
		  int value=-1 ;
		  Scanner sc = new Scanner(System.in);
		  try{
			  value = sc.nextInt();
			  Validator.checkForPositive(value);
		  }catch (InputMismatchException e){
				System.out.println("Input must be integer number.Please enter again.");
				return getInteger();
		  }catch(IllegalArgumentException e){
			    System.out.println("Input must be positive number.Please enter again.");
			    return getInteger();
		  }
		  return value;
	}  
	/**
	 * @param date1
	 * @param date2
	 * @return boolean  
	 * method for date conversion
	 */
	public static boolean dateComprasion(Date date1 , Date date2){
	    Calendar cal1=Calendar.getInstance();
		Calendar cal2=Calendar.getInstance();
		cal1.setTime(date1);
		cal2.setTime(date2);
		return cal1.after(cal2);
	}
}